package com.gocoolmall.seckill.controller;

import com.gocoolmall.common.utils.R;
import com.gocoolmall.seckill.service.SeckillService;
import com.gocoolmall.seckill.to.SeckillSkuRedisTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@Slf4j
@Controller
public class SeckillController {
    @Autowired
    SeckillService seckillService;

    /**
     * 返回当前时间可以参与的秒杀商品信息
     * @return
     */
    @ResponseBody
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus(){
        log.info("currentSeckillSKus正在执行。。。");
        List<SeckillSkuRedisTo>  vos = seckillService.getCurrentSeckillSkus();
        return R.ok().setDate(vos);
    }

    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId){
        SeckillSkuRedisTo to = seckillService.getSkuSeckillInfo(skuId);
        System.out.println("秒杀商品::"+to);
        return R.ok().setDate(to);
    }

    /**
     *
     * @param killId
     * @param key
     * @param num
     * @param model
     * @return
     */
//    @ResponseBody
    @GetMapping("/kill")
    public String seckill(@PathParam("killId") String killId,
                          @PathParam("key") String key,
                          @PathParam("num") Integer num,
                          Model model){
        //1.判断是否登录
        log.info("秒杀上送数据killId："+killId+"   key:"+key+"   num："+num);
        String orderSn = seckillService.kill(killId,key,num);
        model.addAttribute("orderSn",orderSn);
        log.info("秒杀成功返回： "+orderSn);
        //return R.ok().setDate(orderSn);
        return "success";
    }


}
